package com.controllers;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import com.daos.ActivityDao;
import com.daos.CommentDao;
import com.daos.TaskDao;
import com.model.Activity;
import com.model.Employee;
import com.model.Task;
import com.utils.GroupConstants;

@Named
public class TaskController implements Serializable {

	private static final long serialVersionUID = 1L;

	private EmployeeController employeeController;

	private TaskDao taskDao;

	private ActivityDao activityDao;
	
	@Inject
	public TaskController(EmployeeController employeeController,
			TaskDao taskDao, ActivityDao activityDao,CommentDao commentDao) {
		this.employeeController = employeeController;
		this.taskDao = taskDao;
		this.activityDao = activityDao;
	}

	public List<Task> getAllTasks() {
		return taskDao.getAllTasks();
	}

	public List<Task> getTasksForEmployee(int employeeId) {
		return taskDao.getTasksForEmployee(employeeId);
	}

	public List<Task> getTasksForEmployeeByUsername(String username) {
		return taskDao.getTasksForEmployeeByUsername(username);
	}

	public void remove(Task task) {
		task.setVisible(false);
		taskDao.save(task);
		int newProgress = calculateActivityProgress(task, false);
		Activity activity = task.getActivity();
		activity.setProgress(newProgress);
		activityDao.save(activity);
	}

	public void save(Task task) {
		Activity activity = task.getActivity();
		task.setVisible(true);
		activity.getTasks().add(task);
		taskDao.save(task);
		int progress = calculateActivityProgress(task, true);
		activity.setProgress(progress);
		task.setVisible(true);
		saveActivity(activity);
	}

	public List<Activity> getActivitiesForEmployee(int id) {
		return activityDao.getActivitiesForEmployee(id);
	}

	public List<Task> getTasksByActivityId(long id) {
		return taskDao.getTasksByActivityId(id);

	}

	public List<Activity> getAllActivities() {
		return activityDao.getAllActivities();
	}

	public void saveActivity(Activity selectedActivity) {
		selectedActivity.setVisible(true);
		activityDao.save(selectedActivity);
	}

	public int calculateActivityProgress(Task task, boolean includeMe) {
		int progress = 0;
		// Me remuevo a mi mismo si me voy a eliminar
		if (!includeMe) {
			task.getActivity().getTasks().remove(task);
		}
		List<Task> currentTasks = taskDao.getTasksByActivityId(task.getActivity().getId());
		for (Task myTask : currentTasks) {
			// reemplazo con mi nuevo valor para calcular el progress
			if (includeMe && myTask.getId() == task.getId()
					&& myTask.getId() != 0) {
				myTask = task;
			}
			progress += myTask.getProgress();
		}
		int divider = currentTasks.size();
		return divider != 0 ? progress / divider : 0;
	}

	public List<Task> getFinishedTasks(long id) {
		return taskDao.getFinishedTasks(id);
	}

	public List<Task> getCurrentTasks(long id) {
		return taskDao.getCurrentTasks(id);
	}

	public List<Activity> getRelatedActivities(int employeeId) {
		return activityDao.getRelatedActivitiesForEmployee(employeeId);
	}

	public List<Activity> getActivitiesToDisplayForEmployee(
			Employee selectedEmployee, String loggedInUser) {
		Employee loggedIn = employeeController.getLoggedInUser(loggedInUser);
		List<Activity> activities = new ArrayList<Activity>();

		if (loggedIn.getGroup().getName().equalsIgnoreCase(GroupConstants.ADMIN.name())) {
			activities = getActivitiesForEmployee(selectedEmployee.getId());
			
			//Look for Activities with no tasks
			List<Activity> myTasksOfOtherActivities = getRelatedActivities(
					selectedEmployee.getId());
			for (Activity otherActivities : myTasksOfOtherActivities) {
				if (otherActivities.getEmployeeCreator().getId() != selectedEmployee.getId()) {
					activities.add(otherActivities);
				}
			}
		} 
		
		else {
			activities = getActivitiesRelationshipsForUser(
					getRelatedActivities(loggedIn.getId()),
					selectedEmployee.getId());
		}
		
		
		return activities;
	}

	public List<Activity> getActivitiesRelationshipsForUser(
			List<Activity> activities, int selectedEmployeeId) {
		List<Activity> result = new ArrayList<Activity>();
		List<Activity> activitiesOfSelectedUser = getRelatedActivities(selectedEmployeeId);
		for (Activity activity : activities) {
			for (Activity activityOfSelectedUser : activitiesOfSelectedUser) {
				if (activity.equals(activityOfSelectedUser)) {
					result.add(activity);
					break;
				}
			}
		}
		return result;

	}

	public void removeActivity(Activity selectedActivity) {
		selectedActivity.setVisible(false);
		activityDao.save(selectedActivity);
		
	}

	public List<Task> getTasksByActivityIdForEmployee(long id,
			Employee loggedInUser) {
		return taskDao.getTasksByActivityIdForEmployee(id,loggedInUser);
	}

	public List<Activity> getAllActivitiesOrderByTaskDeadLine() {
		return activityDao.getActivitiesForEmployeeOrderByTaskDeadLine();
	}

	public List<Activity> getAllActivitiesWithTasksToBeApproved() {
		return activityDao.getAllActivitiesWithTasksToBeApproved();
	}

	public List<Task> getTasksToBeApprovedOrderByDeadLine() {
		return taskDao.getTasksToBeApprovedOrderByDeadLine();
	}

	public List<Task> getAllDueTasks() {
		return taskDao.getAllTasksWithDueDates();
	}

}
